package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.FlowOS;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;


import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ManageReceiptlistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.RECEIPTLIST";
	
	//传入参数
	private String searchStr = "";
	private String receiptInfo = "";
	private String flowCode = "";
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	
	//传出参数
	private List<ReceiptlistTB> receiptlistList;
	private Map<Integer,RecordlistTB> flowRecordlistMap;
	private Map<String,FlowTB> flowMap;
	
	private Map<Integer,List<ReceiptTB>> receiptListMap;
	
	//各种提示
	private int dayCount;
	private double dayTotal;
	
	private int flowWarehouseStartNum;
	private int flowWarehouseConfirmNum;
	private int flowWarehouseCancelNum;
	private int flowWarehouseDoesconfirmshippriceNum;
	private int flowWarehouseConfirmshippriceNum;
	private int flowWarehouseDoesnotpayNum;
	private int flowWarehouseConfirmpayNum;
	
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		
	
		
		
		 
		ReceiptlistOS receiptlistOS = new ReceiptlistOS();
		
		ReceiptOS receiptOS = new ReceiptOS(receiptlistOS.getConn());
		FlowOS flowOS = new FlowOS(receiptlistOS.getConn());
		Flow flow = new Flow(receiptlistOS.getConn());
		RecordlistOS recordlistOS = new RecordlistOS(receiptlistOS.getConn());

		flowMap = flow.getFlowCodeMapByKind(FlowCode.WAREHOUSE_RECEIPTLIST);
		List<ReceiptTB> receiptInfoList = null;
		if(!receiptInfo.equals("")){
			receiptInfoList = receiptOS.receiptInfoToReceiptList(receiptInfo);
		}
		
		if(!flowCode.equals("")){
			receiptlistList = receiptlistOS.selectByFlowCode(flowCode,FlowCode.WAREHOUSE_RECEIPTLIST,beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNumForFlowCode(flowCode,FlowCode.WAREHOUSE_RECEIPTLIST); 
		}else if(searchStr.equals("") && receiptInfo.equals("")){
			receiptlistList = receiptlistOS.selectReceiptlist(beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNum();
		}else if(!searchStr.equals("") && !receiptInfo.equals("")){//如果出仓信息不为空. 产品信息不为空
			receiptlistList = receiptlistOS.selectBySearchStr(searchStr,receiptInfoList, beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNumForSearchStr(searchStr,receiptInfoList);
			
		}else if(!searchStr.equals("") && receiptInfo.equals("")){//如果出仓信息不为空. 产品信息为空
			receiptlistList = receiptlistOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNumForSearchStr(searchStr);
			
		}else if(searchStr.equals("") && !receiptInfo.equals("")){//如果出仓信息为空. 产品信息不为空
			receiptlistList = receiptlistOS.selectBySearchStr(searchStr,receiptInfoList, beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNumForSearchStr(searchStr,receiptInfoList);
			
		}else{
			receiptlistList = receiptlistOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			rowNum = receiptlistOS.getRowNumForSearchStr(searchStr);
		}
		
		
		/* in Str 组成*/
		String inStr = "(";
		boolean isFirst = true;
		if(receiptlistList == null){
			return null;
		}
		
		for(ReceiptlistTB receiptlist:receiptlistList){
			if(isFirst){
				inStr += receiptlist.getId_receiptlist();
				isFirst = false;
			}else{
				inStr += "," + receiptlist.getId_receiptlist();
			}
		}
		inStr +=")";
		/* in Str 组成  END*/
		
		
		
		flowRecordlistMap = recordlistOS.getRecordlistMapByKindForReport(receiptlistList);
		receiptListMap = receiptOS.getReceiptidListMap(inStr);
		
		//各种提示
		dayCount = receiptlistOS.getTodayCountNum();
		dayTotal = receiptlistOS.getTodayTotal();
		
		
		//flowMap = flowOS.getFlowCodeMapByKind("FLOW.WAREHOUSE.RECEIPTLIST");
		flowWarehouseStartNum = flowMap.get("FLOW.WAREHOUSE.RECEIPTLIST.START").getNumber_flow();
		flowWarehouseConfirmNum = flowMap.get("FLOW.WAREHOUSE.RECEIPTLIST.CONFIRM").getNumber_flow();
		flowWarehouseCancelNum = flowMap.get("FLOW.WAREHOUSE.RECEIPTLIST.CANCEL").getNumber_flow();
		flowWarehouseDoesconfirmshippriceNum = flowMap.get("FLOW.WAREHOUSE.RECEIPTLIST.DOESCONFIRMSHIPPRICE").getNumber_flow();
		//flowWarehouseConfirmshippriceNum = recordlistOS.countFlowNumberByFlowCode("FLOW.WAREHOUSE.RECEIPTLIST.CONFIRMSHIPPRICE");
		flowWarehouseDoesnotpayNum = flowMap.get("FLOW.WAREHOUSE.RECEIPTLIST.DOESNOTPAY").getNumber_flow();
		//flowWarehouseConfirmpayNum = recordlistOS.countFlowNumberByFlowCode("FLOW.WAREHOUSE.RECEIPTLIST.CONFIRMPAY");
		//*/
		receiptlistOS.releaseConnection();
		
		return SUCCESS;
		
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未查看出货单权限.");
			return;
		}
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		this.searchStr = this.searchStr.trim();
	}
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	public List<ReceiptlistTB> getReceiptlistList() {
		return receiptlistList;
	}
	public void setReceiptlistList(List<ReceiptlistTB> receiptlistList) {
		this.receiptlistList = receiptlistList;
	}
	public Map<Integer, RecordlistTB> getFlowRecordlistMap() {
		return flowRecordlistMap;
	}
	public void setFlowRecordlistMap(Map<Integer, RecordlistTB> flowRecordlistMap) {
		this.flowRecordlistMap = flowRecordlistMap;
	}
	public Map<String, FlowTB> getFlowMap() {
		return flowMap;
	}
	public void setFlowMap(Map<String, FlowTB> flowMap) {
		this.flowMap = flowMap;
	}

	public Map<Integer, List<ReceiptTB>> getReceiptListMap() {
		return receiptListMap;
	}

	public void setReceiptListMap(Map<Integer, List<ReceiptTB>> receiptListMap) {
		this.receiptListMap = receiptListMap;
	}

	public int getDayCount() {
		return dayCount;
	}

	public void setDayCount(int dayCount) {
		this.dayCount = dayCount;
	}

	public double getDayTotal() {
		return dayTotal;
	}

	public void setDayTotal(double dayTotal) {
		this.dayTotal = dayTotal;
	}

	public String getReceiptInfo() {
		return receiptInfo;
	}

	public void setReceiptInfo(String receiptInfo) {
		this.receiptInfo = receiptInfo;
	}

	public static String getLimitedcode() {
		return limitedCode;
	}

	public int getFlowWarehouseConfirmNum() {
		return flowWarehouseConfirmNum;
	}

	public void setFlowWarehouseConfirmNum(int flowWarehouseConfirmNum) {
		this.flowWarehouseConfirmNum = flowWarehouseConfirmNum;
	}

	public int getFlowWarehouseCancelNum() {
		return flowWarehouseCancelNum;
	}

	public void setFlowWarehouseCancelNum(int flowWarehouseCancelNum) {
		this.flowWarehouseCancelNum = flowWarehouseCancelNum;
	}

	public int getFlowWarehouseDoesconfirmshippriceNum() {
		return flowWarehouseDoesconfirmshippriceNum;
	}

	public void setFlowWarehouseDoesconfirmshippriceNum(
			int flowWarehouseDoesconfirmshippriceNum) {
		this.flowWarehouseDoesconfirmshippriceNum = flowWarehouseDoesconfirmshippriceNum;
	}

	public int getFlowWarehouseConfirmshippriceNum() {
		return flowWarehouseConfirmshippriceNum;
	}

	public void setFlowWarehouseConfirmshippriceNum(
			int flowWarehouseConfirmshippriceNum) {
		this.flowWarehouseConfirmshippriceNum = flowWarehouseConfirmshippriceNum;
	}

	public int getFlowWarehouseDoesnotpayNum() {
		return flowWarehouseDoesnotpayNum;
	}

	public void setFlowWarehouseDoesnotpayNum(int flowWarehouseDoesnotpayNum) {
		this.flowWarehouseDoesnotpayNum = flowWarehouseDoesnotpayNum;
	}

	public int getFlowWarehouseConfirmpayNum() {
		return flowWarehouseConfirmpayNum;
	}

	public void setFlowWarehouseConfirmpayNum(int flowWarehouseConfirmpayNum) {
		this.flowWarehouseConfirmpayNum = flowWarehouseConfirmpayNum;
	}

	public int getFlowWarehouseStartNum() {
		return flowWarehouseStartNum;
	}

	public void setFlowWarehouseStartNum(int flowWarehouseStartNum) {
		this.flowWarehouseStartNum = flowWarehouseStartNum;
	}

	public String getFlowCode() {
		return flowCode;
	}

	public void setFlowCode(String flowCode) {
		this.flowCode = flowCode;
	}
}
